package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Dugen.Zhu
 * @date 2020-01-02 11:50
 */
@Controller
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传表格
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0) {
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] row : list) {
                    String data = row[0];
                    String number = row[1];
                    OrderSetting orderSetting = new OrderSetting(new SimpleDateFormat("yyyy/MM/dd").parse(data),Integer.parseInt(number));
                    orderSettingList.add(orderSetting);
                }
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据月份查询预约数据
     *
     * @param date
     * @return
     */
    @RequestMapping("/findOrderSettingByMonth")
    @ResponseBody
    public Result findOrderSettingByMonth(String date) {
        try {
            //查询到的预约结果列表
            List<OrderSetting> orderSettingList = orderSettingService.findOrderSettingByMonth(date);
            //最终要返回的结果集，可以被前端页面解析
            List<Map> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                HashMap hashMap = new HashMap();
                hashMap.put("date", orderSetting.getOrderDate().getDate());
                hashMap.put("number", orderSetting.getNumber());
                hashMap.put("reservations", orderSetting.getReservations());
                mapList.add(hashMap);
            }
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期设置预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/setNumberByDate")
    @ResponseBody
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.setNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
